package com.hgb.mall.mbg.mapper;

import com.hgb.mall.mbg.entity.PmsBrand;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.delete.DeleteDSLCompleter;
import org.mybatis.dynamic.sql.delete.render.DeleteStatementProvider;
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
import org.mybatis.dynamic.sql.select.CountDSLCompleter;
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
import org.mybatis.dynamic.sql.update.UpdateDSL;
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
import org.mybatis.dynamic.sql.update.UpdateModel;
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
import org.mybatis.dynamic.sql.util.mybatis3.MyBatis3Utils;

import javax.annotation.Generated;
import java.util.List;
import java.util.Optional;

import static com.hgb.mall.mbg.mapper.PmsBrandDynamicSqlSupport.*;
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;

@Mapper
public interface PmsBrandMapper {
    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    BasicColumn[] selectList = BasicColumn
            .columnList(id, name, firstLetter, sort, factoryStatus, showStatus, productCount, productCommentCount, logo, bigPic, brandStory);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateAllColumns(PmsBrand record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(name).equalTo(record::getName)
                  .set(firstLetter).equalTo(record::getFirstLetter)
                  .set(sort).equalTo(record::getSort)
                  .set(factoryStatus).equalTo(record::getFactoryStatus)
                  .set(showStatus).equalTo(record::getShowStatus)
                  .set(productCount).equalTo(record::getProductCount)
                  .set(productCommentCount).equalTo(record::getProductCommentCount)
                  .set(logo).equalTo(record::getLogo)
                  .set(bigPic).equalTo(record::getBigPic)
                  .set(brandStory).equalTo(record::getBrandStory);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    static UpdateDSL<UpdateModel> updateSelectiveColumns(PmsBrand record, UpdateDSL<UpdateModel> dsl) {
        return dsl.set(name).equalToWhenPresent(record::getName)
                  .set(firstLetter).equalToWhenPresent(record::getFirstLetter)
                  .set(sort).equalToWhenPresent(record::getSort)
                  .set(factoryStatus).equalToWhenPresent(record::getFactoryStatus)
                  .set(showStatus).equalToWhenPresent(record::getShowStatus)
                  .set(productCount).equalToWhenPresent(record::getProductCount)
                  .set(productCommentCount).equalToWhenPresent(record::getProductCommentCount)
                  .set(logo).equalToWhenPresent(record::getLogo)
                  .set(bigPic).equalToWhenPresent(record::getBigPic)
                  .set(brandStory).equalToWhenPresent(record::getBrandStory);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    long count(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @DeleteProvider(type = SqlProviderAdapter.class, method = "delete")
    int delete(DeleteStatementProvider deleteStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @InsertProvider(type = SqlProviderAdapter.class, method = "insert")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "record.id", before = false, resultType = Long.class)
    int insert(InsertStatementProvider<PmsBrand> insertStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @ResultMap("PmsBrandResult")
    Optional<PmsBrand> selectOne(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @SelectProvider(type = SqlProviderAdapter.class, method = "select")
    @Results(id = "PmsBrandResult", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "first_letter", property = "firstLetter", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "factory_status", property = "factoryStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "show_status", property = "showStatus", jdbcType = JdbcType.INTEGER),
            @Result(column = "product_count", property = "productCount", jdbcType = JdbcType.INTEGER),
            @Result(column = "product_comment_count", property = "productCommentCount", jdbcType = JdbcType.INTEGER),
            @Result(column = "logo", property = "logo", jdbcType = JdbcType.VARCHAR),
            @Result(column = "big_pic", property = "bigPic", jdbcType = JdbcType.VARCHAR),
            @Result(column = "brand_story", property = "brandStory", jdbcType = JdbcType.LONGVARCHAR)
    })
    List<PmsBrand> selectMany(SelectStatementProvider selectStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    @UpdateProvider(type = SqlProviderAdapter.class, method = "update")
    int update(UpdateStatementProvider updateStatement);

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default long count(CountDSLCompleter completer) {
        return MyBatis3Utils.countFrom(this::count, pmsBrand, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int delete(DeleteDSLCompleter completer) {
        return MyBatis3Utils.deleteFrom(this::delete, pmsBrand, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int deleteByPrimaryKey(Long id_) {
        return delete(c ->
                c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insert(PmsBrand record) {
        return MyBatis3Utils.insert(this::insert, record, pmsBrand, c ->
                c.map(name).toProperty("name")
                 .map(firstLetter).toProperty("firstLetter")
                 .map(sort).toProperty("sort")
                 .map(factoryStatus).toProperty("factoryStatus")
                 .map(showStatus).toProperty("showStatus")
                 .map(productCount).toProperty("productCount")
                 .map(productCommentCount).toProperty("productCommentCount")
                 .map(logo).toProperty("logo")
                 .map(bigPic).toProperty("bigPic")
                 .map(brandStory).toProperty("brandStory")
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int insertSelective(PmsBrand record) {
        return MyBatis3Utils.insert(this::insert, record, pmsBrand, c ->
                c.map(name).toPropertyWhenPresent("name", record::getName)
                 .map(firstLetter).toPropertyWhenPresent("firstLetter", record::getFirstLetter)
                 .map(sort).toPropertyWhenPresent("sort", record::getSort)
                 .map(factoryStatus).toPropertyWhenPresent("factoryStatus", record::getFactoryStatus)
                 .map(showStatus).toPropertyWhenPresent("showStatus", record::getShowStatus)
                 .map(productCount).toPropertyWhenPresent("productCount", record::getProductCount)
                 .map(productCommentCount).toPropertyWhenPresent("productCommentCount", record::getProductCommentCount)
                 .map(logo).toPropertyWhenPresent("logo", record::getLogo)
                 .map(bigPic).toPropertyWhenPresent("bigPic", record::getBigPic)
                 .map(brandStory).toPropertyWhenPresent("brandStory", record::getBrandStory)
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<PmsBrand> selectOne(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectOne(this::selectOne, selectList, pmsBrand, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<PmsBrand> select(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectList(this::selectMany, selectList, pmsBrand, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default List<PmsBrand> selectDistinct(SelectDSLCompleter completer) {
        return MyBatis3Utils.selectDistinct(this::selectMany, selectList, pmsBrand, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default Optional<PmsBrand> selectByPrimaryKey(Long id_) {
        return selectOne(c ->
                c.where(id, isEqualTo(id_))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int update(UpdateDSLCompleter completer) {
        return MyBatis3Utils.update(this::update, pmsBrand, completer);
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKey(PmsBrand record) {
        return update(c ->
                c.set(name).equalTo(record::getName)
                 .set(firstLetter).equalTo(record::getFirstLetter)
                 .set(sort).equalTo(record::getSort)
                 .set(factoryStatus).equalTo(record::getFactoryStatus)
                 .set(showStatus).equalTo(record::getShowStatus)
                 .set(productCount).equalTo(record::getProductCount)
                 .set(productCommentCount).equalTo(record::getProductCommentCount)
                 .set(logo).equalTo(record::getLogo)
                 .set(bigPic).equalTo(record::getBigPic)
                 .set(brandStory).equalTo(record::getBrandStory)
                 .where(id, isEqualTo(record::getId))
        );
    }

    @Generated("org.mybatis.generator.api.MyBatisGenerator")
    default int updateByPrimaryKeySelective(PmsBrand record) {
        return update(c ->
                c.set(name).equalToWhenPresent(record::getName)
                 .set(firstLetter).equalToWhenPresent(record::getFirstLetter)
                 .set(sort).equalToWhenPresent(record::getSort)
                 .set(factoryStatus).equalToWhenPresent(record::getFactoryStatus)
                 .set(showStatus).equalToWhenPresent(record::getShowStatus)
                 .set(productCount).equalToWhenPresent(record::getProductCount)
                 .set(productCommentCount).equalToWhenPresent(record::getProductCommentCount)
                 .set(logo).equalToWhenPresent(record::getLogo)
                 .set(bigPic).equalToWhenPresent(record::getBigPic)
                 .set(brandStory).equalToWhenPresent(record::getBrandStory)
                 .where(id, isEqualTo(record::getId))
        );
    }
}
